package activities;


import java.util.ArrayList;

import diplomski.osijekinfo.R;





import baza.Bazapodataka;
import baza.Objekti;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Adapterlist extends Activity {

    ListView l1;
    String naslov;
    TextView tvNaslov;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent intent = getIntent();
        naslov = intent.getStringExtra("naslov");
        setContentView(R.layout.listview);
        int id_ustanova= intent.getIntExtra("id", 1);
        
        
        l1=(ListView)findViewById(R.id.list);
        
        Bazapodataka baza= new Bazapodataka(this);
        baza.otvori();
        ArrayList<Objekti> objekti= baza.dohvatiObjekti(id_ustanova);
        baza.zatvori();
        dataListAdapter data= new dataListAdapter(objekti);
        l1.setAdapter(data);
        
    
      
        tvNaslov= (TextView)findViewById(R.id.naslov);
        tvNaslov.setText(naslov);
    }

    class dataListAdapter extends BaseAdapter {
    	
        ArrayList<Objekti> objekti;        

        public dataListAdapter(ArrayList<Objekti> objekti) {
            this.objekti=objekti;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return objekti.size();
        }

        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
        	final int fpostition=position;
            LayoutInflater inflater = getLayoutInflater();
            View row;
            row = inflater.inflate(R.layout.customlistview, parent, false);
            
            TextView nazivi_objekata;
          
            
            nazivi_objekata = (TextView) row.findViewById(R.id.naziviobjekata);
           
            
           nazivi_objekata.setText(objekti.get(position).getNazivObjekata());
           row.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Intent myIntent = new Intent (Adapterlist.this, Opis.class);
				Objekti objekt=objekti.get(fpostition);
				myIntent.putExtra("objekt", objekt);
				 startActivity(myIntent);
			}
           });
           

            return row;
        }
    }
}
